﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using CmCaptureOcxLib;

namespace KSOAPlus
{
    public partial class Scanner : Form
    {
        public Scanner()
        {
            InitializeComponent();
        }

        Common Init = new Common();

        public class Info
        {
            public string value { get; set; }
            public string text { get; set; }
        }

        public string Path="Images";

        private void Scanner_Load(object sender, EventArgs e)
        {            
            int i = 0;
            int total = axCmCaptureOcx1.GetDevCount();
            for (i = 0; i < total; i++)
            {
                var DevEle = axCmCaptureOcx1.GetDevFriendName(i);
                cbDeviceName.Items.Add(DevEle);
                cbDeviceName.SelectedIndex = 0;  
            }
            
            if (total > 0)
            {
                SelectCamera(0);    
            }
             
            List<Info> infoList = new List<Info>();
            infoList.Add(new Info() { value = "0", text = "BMP" });
            infoList.Add(new Info() { value = "1", text = "JPG" });
            infoList.Add(new Info() { value = "2", text = "TIF" });
            infoList.Add(new Info() { value = "3", text = "PNG" });
            cbFileType.DataSource = infoList;
            cbFileType.ValueMember = "value";
            cbFileType.DisplayMember = "text";
            cbFileType.SelectedIndex = 1;

            infoList.Clear();
            infoList.Add(new Info() { value = "yyzz", text = "营业执照" });
            infoList.Add(new Info() { value = "ypzlgf", text = "经营/生产许可证" });
            infoList.Add(new Info() { value = "gspzs", text = "GSP/GMP证书" });
            infoList.Add(new Info() { value = "zlbzxy", text = "质量保证协议" });
            infoList.Add(new Info() { value = "yzyp", text = "样张样票" });
            infoList.Add(new Info() { value = "fpyz", text = "发票样张" });
            infoList.Add(new Info() { value = "yinzhang", text = "单位印章" });
            infoList.Add(new Info() { value = "frwts", text = "法人委托书" });
            infoList.Add(new Info() { value = "bwtrsfz", text = "被委托人身份证" });
            infoList.Add(new Info() { value = "khxkz", text = "开户许可证" });
            infoList.Add(new Info() { value = "zlbznldcb", text = "质量保证能力调查表" });
            infoList.Add(new Info() { value = "dwbgjl", text = "单位变更记录" });
            infoList.Add(new Info() { value = "kpxx", text = "开票信息" });
            infoList.Add(new Info() { value = "dbthxkpj", text = "蛋白同化制剂、肽类激素许可批件" });
            infoList.Add(new Info() { value = "yljgzyxkz", text = "医疗机构执业许可证" });
            infoList.Add(new Info() { value = "ylqxjyxkz", text = "医疗器械经营许可证" });
            infoList.Add(new Info() { value = "spltxkz", text = "食品经营许可证" });
            infoList.Add(new Info() { value = "qyndgsb", text = "企业年度公示表" });
            infoList.Add(new Info() { value = "swdlz", text = "税务登记证" });
            infoList.Add(new Info() { value = "hetong", text = "合同" });
            cbType.DataSource = infoList;
            cbType.ValueMember = "value";
            cbType.DisplayMember = "text";
            //cbType.SelectedIndex = 1;
        }
        string corpid = "";
        private void button2_Click(object sender, EventArgs e)
        {
            string typeid = cbType.SelectedValue.ToString();
            string url = Init.ServerURL;
            string endrq = dTP.Text;
            if (string.IsNullOrEmpty(corpid))
            {
                MessageBox.Show("请选择单位！");
                txtdanwbh.Focus();
                return;
            }
            
            if (string.IsNullOrEmpty(typeid))
            {
                MessageBox.Show("请选择资料类别！");
                cbType.Focus();
                return;
            }
            
            
           string CaptureToBase64 = axCmCaptureOcx1.CaptureToBase64();
           bool isup = Init.Up(url, CaptureToBase64, corpid, typeid, endrq, 0);
            if(isup)
            {
                MessageBox.Show("上传成功！");
            }
            else{
                MessageBox.Show("上传失败！");
            }
        }

        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            int obj = cbDeviceName.SelectedIndex;
            axCmCaptureOcx1.Initial();
            axCmCaptureOcx1.RotateVideo(3);
            axCmCaptureOcx1.StartRun(obj);
            
            if (cbResolution.Items.Count == 0)
            {
                AddResolution2Comb();
            }
            SetResolution();            
        }
        public void SelectCamera(int obj)
        {
            axCmCaptureOcx1.Initial();
            axCmCaptureOcx1.RotateVideo(3);
            axCmCaptureOcx1.StartRun(obj);

            if (cbResolution.Items.Count == 0)
            {
                AddResolution2Comb();
            }
            SetResolution();
        }
        /// <summary>
        /// 添加分辨率
        /// </summary>
        public void AddResolution2Comb()
        {
            var i = 0;
            var total = axCmCaptureOcx1.GetResolutionCount();
            for (i = 0; i < total; i++)
            {
                var resolution = axCmCaptureOcx1.GetResolution(i);
                cbResolution.Items.Add(resolution);
            }
        }
        /// <summary>
        /// 分辨率设置
        /// </summary>
        public void SetResolution()
        {
            int obj = cbResolution.SelectedIndex;
            axCmCaptureOcx1.SetResolution(obj);
        }
        /// <summary>
        /// 文件类型
        /// </summary>
        public void SetFileType()
        {
            int obj = cbFileType.SelectedIndex;
            axCmCaptureOcx1.SetFileType(obj);
        }

        int bAdjustMode = 0;

        /// <summary>
        /// 自动剪裁
        /// </summary>
        public void Adjust()
        {
            if (bAdjustMode == 0)
            {
                axCmCaptureOcx1.AutoCrop(1);
                bAdjustMode = 1;
            }
            else
            {
                axCmCaptureOcx1.AutoCrop(0);
                bAdjustMode = 0;
            }
        }
        int bSetDenoise = 0;

        /// <summary>
        /// 消除噪点
        /// </summary>
        public void SetDenoise()
        {
            if (bSetDenoise == 0)
            {
                bSetDenoise = 1;
                axCmCaptureOcx1.SetDenoise(bSetDenoise);
            }
            else
            {
                bSetDenoise = 0;
                axCmCaptureOcx1.SetDenoise(bSetDenoise);
            }
        }

        string strFile;
        int nFileCount = 0;
        /// <summary>
        /// 拍照
        /// </summary>
        public void XCapture()
        {
            int obj = cbFileType.SelectedIndex;
            if (!Init.folderExist(Path))
            {
                Init.folderCreate(Path);
            }

            strFile = @Path + "\\" + nFileCount;

            if (obj == 0)
            {
                strFile += ".bmp";
            }
            if (obj == 1)
            {
                strFile += ".jpg";
            }
            if (obj == 2)
            {
                strFile += ".tif";
            }
            if (obj == 3)
            {
                strFile += ".png";
            }
            //CmCaptureOcx.SetJpgQuanlity(10);//设置图片压缩率
            axCmCaptureOcx1.CaptureImage(strFile);
            nFileCount++;
        }

        private void btnAdjust_Click(object sender, EventArgs e)
        {
            Adjust();
        }

        private void btnCapture_Click(object sender, EventArgs e)
        {
            XCapture();
        }
        int Rotate = 0;
        private void button5_Click(object sender, EventArgs e)
        {
            Rotate++;
            if (Rotate > 3)
            {
                Rotate = 0;
            }
            axCmCaptureOcx1.RotateVideo(Rotate);
            btnRotate.Text = "右旋转" + Rotate.ToString();
        }

        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axCmCaptureOcx1.ShowImageSettingWindow();
        }

        int bCropArea = 0;
        private void btnCusCrop_Click(object sender, EventArgs e)
        {
            if (bCropArea == 0)
            {
                bCropArea = 1;
                axCmCaptureOcx1.CusCrop(bCropArea);
            }
            else
            {
                bCropArea = 0;
                axCmCaptureOcx1.CusCrop(bCropArea);
            }
        }        

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter || e.KeyData == Keys.Return)
            {
                SelDw();
            }
        }

        private void btnDenoise_Click(object sender, EventArgs e)
        {
            SetDenoise();
        }

        private void txtdanwbh_DoubleClick(object sender, EventArgs e)
        {
            SelDw();
        }

        public void SelDw()
        {
            Instance.Initialize();
            Instance.instance.dwbh = "";
            Instance.instance.danwbh = "";
            Instance.instance.dwmch = "";
            SelectDw SelectDw = new SelectDw();
            SelectDw.querystr = txtdanwbh.Text;
            SelectDw.ShowInTaskbar = false;
            SelectDw.ShowDialog();
            txtdanwbh.Text = Instance.instance.danwbh;
            txtdwmch.Text = Instance.instance.dwmch;
            corpid = Instance.instance.dwbh;
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            SysConfig SysConfig = new SysConfig();
            SysConfig.ShowDialog();
        }

    }
}